package com.main.heath.controller;

import com.alibaba.fastjson.JSONObject;
import com.main.heath.controller.Json.MyJsonResponse;
import com.main.heath.po.PingData;
import com.main.heath.response.Response;
import com.main.heath.servcie.PingControlService;
import com.main.heath.servcie.PingDataService;
import com.main.heath.servcie.impl.PingControlServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by heathhose on 16-11-15.
 */
@Controller
public class PingDataController {
    @Autowired
    private PingDataService pingDataService;
    @Autowired
    private PingControlService pingControlService;

    @RequestMapping("/getPingInfo")
    @ResponseBody
    public void getPingInfo(String ipAddress,HttpServletRequest request, HttpServletResponse response) throws Exception {
        PingData pingdata = pingDataService.getPingInfo(ipAddress);
        if(pingdata != null){
            MyJsonResponse.responsePojo(response,pingdata);
        }
        else{
            MyJsonResponse.responseObject(response,"Code","null");
        }
    }

    @RequestMapping("/getAllPingInfo")
    public void getAllPingInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<PingData> list = pingDataService.getAllPingInfo();
        MyJsonResponse.responseList(response,list);
    }

    @RequestMapping("/startPing")
    @ResponseBody
    public void startPing(String ipAddress,HttpServletRequest request,HttpServletResponse response) throws Exception {
//        ipAddress =  "baidu.com";
        MyJsonResponse.responseObject(response,"Code",pingControlService.startPing(ipAddress));

    }

    @RequestMapping("/endPing")
    public  void endPing(String ipAddress,HttpServletRequest request,HttpServletResponse response) throws Exception {
//        ipAddress =  "123.125.114.144";
//        ipAddress = "10.0.45.34";
        MyJsonResponse.responseObject(response,"Code",pingControlService.endPing(ipAddress));
    }

    @RequestMapping("/deleteAllPingInfo")
    public  void deleteAllPingInfo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        MyJsonResponse.responseObject(response,"Code",pingDataService.deleteAllPingInfo());
    }

    @RequestMapping("/getPingTimeAnalysis")
    @ResponseBody
    //获取对PingTime的分析`
    public void getPingTimeAnalysis(String ipAddress,HttpServletRequest request,HttpServletResponse response) throws Exception{
//        ipAddress = "123.125.114.144";
        MyJsonResponse.responseJsonObject(response,pingDataService.analysisPing(ipAddress));
    }

    @RequestMapping("/analyPacketLoss")
    @ResponseBody
    //获取丢包率
    public void analyPacketLoss(String ipAddress,HttpServletRequest request,HttpServletResponse response) throws Exception{
//        ipAddress = "123.125.114.144";
        MyJsonResponse.responseObject(response,"packetLoss",pingDataService.analyPacketLoss(ipAddress));
    }

    @RequestMapping("/analyReachable")
    @ResponseBody
    //获取connection
    public void analyReachable(String ipAddress,HttpServletRequest request,HttpServletResponse response) throws Exception{
//        ipAddress = "123.125.114.144";
        MyJsonResponse.responseObject(response,"reachable",pingDataService.analyReachable(ipAddress));
    }
}
